package com.sinodata.bsm.itip.collector;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.jms.JMSException;

import org.apache.log4j.Logger;

import com.sinodata.bsm.common.vo.Res;
import com.sinodata.bsm.common.vo.ResPropertyValue;
import com.sinodata.bsm.itip.cache.CacheFactory;
import com.sinodata.bsm.itip.cache.ResCache;
import com.sinodata.bsm.itip.message.Messenger;
import com.sinodata.bsm.itip.util.DB2ConnectionUtil;

public class UnixCollector {

    private static final Logger logger = Logger.getLogger(UnixCollector.class);

    private ResCache resCache = (ResCache) CacheFactory.getCache(ResCache.class);

    /**
     * 查询指标  保留有资源对应的指标  并通过activemq发送给center
     * @param beginTime
     * @param endTime
     * @param groupType
     */
    public void execute(String beginTime, String endTime, String groupType) {
        logger.info("[TIP]\t Fetch Props From Tivoli (unixcollector)");

        System.out.println("collect tivoli");
        List<Res> resList = resCache.getResByTypeGroup(groupType);
        List<String[]> propList = fetchPorpFromWarehous(groupType, beginTime, endTime);

        List<ResPropertyValue> values = check(propList, resList);

        for (ResPropertyValue value : values) {

            sendPropertyValue(value);
        }
    }

    /**
     * 对取到的指标值做校验  如果指标值有对应的资源则保留 否则删除 
     * @param propList
     * @param resList
     * @return
     */
    List<ResPropertyValue> check(List<String[]> propList, List<Res> resList) {
        List<ResPropertyValue> list = new ArrayList<ResPropertyValue>();
        return list;
    }

    /**
     * 查询指标数据
     * @param groupType
     * @param begintIME
     * @param endTime
     * @return
     */
    @SuppressWarnings("unused")
    public List<String[]> fetchPorpFromWarehous(String groupType, String beginTime, String endTime) {
        List<String[]> propList = new ArrayList<String[]>();

        StringBuilder sb = new StringBuilder("select * from ");
        String table = getTableName(groupType);
        sb.append(table);
        sb.append(" where p.xxx > to_date('" + beginTime + "','yyyy-MM-dd hh24:mi:ss')");
        sb.append(" and p.xxx < to_date('" + endTime + "','yyyy-MM-dd hh24:mi:ss')");

        Connection conn = null;
        Statement statement = null;
        ResultSet rs = null;
        try {
            conn = DB2ConnectionUtil.getInstance().getConnection();
            statement = conn.createStatement();
            rs = statement.executeQuery(sb.toString());
            ResultSetMetaData rsm = rs.getMetaData();
            int count = rsm.getColumnCount();
            while (rs.next()) {

            }
        } catch (Exception e) {
            return null;
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException ex) {
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                }
            }
            DB2ConnectionUtil.getInstance().returnConn(conn);
        }

        return propList;
    }

    /**
     * 根据资源类别组 获取  表名
     * @param type
     * @return
     */
    String getTableName(String type) {
        return "kfwisit";
    }

    /**
     * 发送指标数据
     * @param valuex
     */
    private void sendPropertyValue(ResPropertyValue valuex) {
        System.out.println(valuex);
        try {
            Messenger.getInstance().sendPropertyValue(valuex);
        } catch (JMSException e) {
            logger.error("failed to send property value:" + valuex, e);
        }
    }

}
